package com.wolfalpha.utron.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.bean.InformationComment;
import com.wolfalpha.utron.utils.StringUtils;

import java.util.List;

/**
 * （资讯评论）列表适配器
 * <p/>
 * Created by luodon on 15/8/19.
 */
public class ItemInformationCommentAdapter extends BaseAdapter {


    private LayoutInflater inflater;
    private Context context = null;
    private List<InformationComment> comments;

    public ItemInformationCommentAdapter(Context context, List<InformationComment> rs) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        comments = rs;
    }

    /**
     * 改变数据
     *
     * @param rs
     */
    public void setData(List<InformationComment> rs) {
        comments = rs;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return comments.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_information_comment, null);
            viewHolder = new ViewHolder();
            viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.item_tv_information_comment_user_name);
            viewHolder.tvContent = (TextView) convertView.findViewById(R.id.item_tv_information_comment_content);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.item_tv_information_comment_time);
            viewHolder.ivHead = (ImageView) convertView.findViewById(R.id.item_iv_information_comment_user_head);
            viewHolder.ivHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) mListener.clickHead(comments.get(position).getUser_id());
                }
            });
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String name = comments.get(position).getUser_name();
        if (comments.get(position).getTo_id() > 0) {
            name = name + " 回复 " + comments.get(position).getTo_name();
        }
        //设置控件内容
        viewHolder.tvUserName.setText(name);
        viewHolder.tvContent.setText(comments.get(position).getContent());
        viewHolder.tvTime.setText(String.valueOf(StringUtils.friendly_time(StringUtils.TimeStamp2Date(comments.get(position).getTime() * 1000))));

      /*  String imageUri = "http://site.com/image.png";      // from Web
        String imageUri = "file:///mnt/sdcard/image.png";   // from SD card
        String imageUri = "content://media/external/audio/albumart/13"; // from content provider
        String imageUri = "assets://image.png";*/
//        ImageLoader.getInstance().displayImage(informations.get(position).getPic(), viewHolder.ivPic, options);
//        viewHolder.ivPic.setTag("informations.get(position).getPic()");
        //设置图片
        Picasso.with(context)
                .load(comments.get(position).getUser_head())
                .placeholder(R.mipmap.head_default)
                .into(viewHolder.ivHead);
        return convertView;
    }

    /**
     * 控件
     */
    class ViewHolder {
        private TextView tvUserName;    //用户昵称
        private TextView tvContent;     //回复内容
        private TextView tvTime;        //回复时间
        private ImageView ivHead;       //头像地址
    }

    /**
     * 点击头像事件
     */
    public interface InformationCommentListener {
        public void clickHead(int userId);
    }

    private InformationCommentListener mListener;

    /**
     * 设置监听器
     */
    public void setInformationCommentListener(InformationCommentListener listener) {
        mListener = listener;
    }
}
